<?php

class WBProjectLoader extends PObject {

	private $_rootWindow = null;
	
	public function __construct($xml) { 
		$this->createProject($xml);
	}
	
	private function createProject($xml) { 
		
		$project = simplexml_load_string($xml);
		
		$i = 0;
		foreach ($project->windows->window as $window) { 
			$options = $this->createControl($window);
			
			$win = wbcreate($options);
			
			if ($i == 0) { 
				$this->_rootWindow = $win;
			}
			
			$i++;
		}
		
	}
	
	private function createControl($obj) { 
		$options = array();
		foreach ($obj->options->children() as $option) {
			if ($option->getName() == 'layout') {  
				$options['layout'] = array('layout' => (string)$option );
				foreach ($option->attributes() as $key => $value) { 
					$options['layout'][$key] = $value;
				}
			} else if ($option->getName() == 'event') {  
				$options['event'] = array();
				foreach ($option->children() as $event) { 
					$options['event'][$event->getName()] = (string)$event;
				}
				
			} else { 
				$options[$option->getName()] = (string)$option;
			}
		}
		
		if ($obj->controls->count() > 0) { 
		
			$options['controls'] = array();
			
			foreach ($obj->controls->control as $sub) { 
				$options['controls'][] = $this->createControl($sub);
			}
		}		
		
		return $options;
	}
	
	public static function loadXml($path) { 
		return new WBProjectLoader(file_get_contents($path));
	}
	
	public function getRootWindow() { 
		return $this->_rootWindow;
	}
}

?>